﻿using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using GameLogic;
using UnityEngine.UI;

namespace GameCore
{
    /// <summary>
    /// 面板,所有的窗体必须继承该类
    /// </summary>
    [RequireComponent(typeof(Canvas))]
    [RequireComponent(typeof(GraphicRaycaster))]
    public class Panel :UIBase
    {

        protected Canvas canvas;
        protected GraphicRaycaster graphicRaycaster;

        protected override void InitComponents()
        {
            base.InitComponents();
            this.canvas = this.gameObject.AddMissingComponent<Canvas>();
            this.graphicRaycaster = this.gameObject.AddMissingComponent<GraphicRaycaster>();
        }

        /// <summary>
        /// 设置排序值
        /// </summary>
        /// <param name="sortingOrder"></param>
        public void SetSortingOrder(int sortingOrder) {
            this.canvas.overrideSorting = true;
            this.canvas.sortingOrder = sortingOrder;
        }
        /// <summary>
        /// 打开界面 PS:注册监听,刷新显示等等
        /// </summary>
        public virtual void Open()
        {
            if (this.gameObject.activeSelf)
                return;
            this.gameObject.SetActive(true);
        }


        /// <summary>
        /// 关闭界面 PS:注销监听等等
        /// </summary>
        public virtual void Close()
        {
            this.gameObject.SetActive(false);
        }


        /// <summary>
        /// 隐藏面板
        /// </summary>
        public void HidePanel() {
            App.PanelMgr.ClosePanel(this.GetType().Name, false);
        }

        /// <summary>
        /// 关闭面板
        /// </summary>
        public void ClosePanel() {
            App.PanelMgr.ClosePanel(this.GetType().Name,true);
        }
    }
}

